import {
  get,
  post
} from '../../request/index'
const AUTH = require('../../utils/auth')
const {
  typeJson
} = require('../../utils/typejson')

Component({
  /**
   * 组件的属性列表
   */
 options:{
  multipleSlots: true // 在
 }, 
  lifetimes: {
    ready: function () {
      // 在组件实例进入页面节点树时执行
      try {
        this.getList()
        this.initSlideButton()
      } catch (error) {

      }





    },

  },

  properties: {
    disable: {
      type: Boolean,
      value: ""
    },
    pulldown: {
      type: Number,
      observer: function () {
        this.getList()
      }
    },
    pullup: {
      type: Number,
      observer: function (oldVal, newVal) {

        this.appendList(newVal)
      }
    },
    Refresh: {
      type: Boolean,
      observer: function (oldVal, newVal) {
        this.getList()
      }
    },

    addButton: {
      type: Boolean,
      value: ""
    },
    identifShow: {
      type: Boolean,
      value: false
    },
    listConfig: {
      type: Array,
      value: [{
          label: '辨识类型',
          prop: 'riskUnitName'
        },
        {
          label: '风险等级',
          prop: 'riskLevelName'
        }
      ]
    },


    slide: {
      type: Object,
      value: {}
    },

    formConfig: {
      type: Array,
      value: []
    },

    net: {
      type: Object,
      value: {
        select: {
          url: '',
          method: 'post',
          data: {
            mineId: true
          }
        },
        delete: {
          url: '',
          method: 'post',
          data: {

          }
        },
        insert: {
          url: '',
          method: 'post',
          data: {
            mineId: true
          }
        },
        update: {
          url: '',
          method: 'post',
          data: {

          }
        }

      }
    }


  },


  /**
   * 组件的初始数据
   */
  data: {
    more: false,
    pageSize: 0,
    list: [],
    total: 1,
    slideButtons: [],
    loadingImg: true,
    show: false,
    typeShow: null,
    objProp: {},
    closeId: null,
   
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onClose() {
      this.setData({
        show: false
      })

      this.triggerEvent('showClose', {
        show: false
      });

    },
    initSlideButton() {

      if (!this.properties.slide.slideButton) {
        return
      }
      let slideButtons = this.data.slideButtons
      slideButtons.push(this.properties.slide.slideButton)
      this.setData({
        slideButtons: slideButtons
      })

    },

    async appendList(currentPage) {


      if (this.data.total == this.data.list.length) {
        this.setData({
          more: true

        })


        return
      }
      if (currentPage == 0 || currentPage == 1) {
        return
      }
      const {
        url,
        method,
        data
      } = this.properties.net.select
      console.log(this.properties.net);

      let currentUrl = url + "?currentPage=" + currentPage + "&pageSize=10"

      if (data.mineId) {
        data.mineId = AUTH.getMineId()
      }

      for (let x in data) {
        if (data[x] == "userid") {
          let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
          data[x] = prolist.userid
        }
      }

      if (method == 'get') {
        let res = await get(currentUrl, data)
        this.addList(res)

      } else {

        let res = await post(currentUrl, data)

        this.addList(res)
      }
    },

    addList(res) {
      const {
        total
      } = res.data
      console.log(total);
      wx.stopPullDownRefresh()
      if (!res.success) {
        return
      }
      let {
        list
      } = this.data
      this.setData({
        list: list.concat(res.data.list),
        total: total,
        loadingImg: false
      })

    },

    async getList() {
      const {
        url,
        method,
        data
      } = this.properties.net.select

      if (data.mineId) {
        data.mineId = AUTH.getMineId()
      }

      for (let x in data) {
        if (data[x] == "userid") {
          let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
          data[x] = prolist.userid
        }
      }

      if (method == 'get') {
        let res = await get(url + "?currentPage=1&pageSize=10", data)
        this.setList(res)
      }
      if (method == 'post') {
        let res = await post(url + "?currentPage=1&pageSize=10", data)

        this.setList(res)
      }

    },

    setList(res) {

      wx.stopPullDownRefresh()
      if (!res.success) {
        return
      }
      this.setData({
        list: res.data.list,
        total: res.data.total,
        loadingImg: false
      })
    },

    afterDelete(res) {
      if (res.success == true) {
        this.getList()
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 1500,
          mask: true
        })
      } else {
        wx.showToast({
          title: '删除失败',
          icon: 'none',
          duration: 1500,
          mask: true
        })
      }
    },

    async slideButtonTap(e) {
      console.log(e);
      const {
        url,
        method,
        data
      } = this.properties.net.delete;
      const id = e.currentTarget.dataset.data.id
      let index = e.detail.index;
      var flag = false

      if (index == 0) {
        this.data.formConfig.forEach(function (config) {

          if (config.tag == 'userid') {
            let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
            let num = parseInt(prolist.userid)

            console.log(num, e.currentTarget.dataset.data[config.prop]);
            if (num != e.currentTarget.dataset.data[config.prop]) {
              wx.showToast({
                title: '编辑失败,没有权限',
                icon: 'none',
                duration: 1500,
                mask: true
              })
              flag = true
            }
          }
        })


        if (flag) return
        let data = JSON.stringify(e.currentTarget.dataset.data)
        let formConfig = JSON.stringify(this.properties.slide.formConfig)
        let net = JSON.stringify(this.data.net.update)
        let form = JSON.parse(formConfig)
        wx.navigateTo({
          url: `/pages/edit/edit?formConfig=${formConfig}&data=${data}&net=${net}&titile=${this.data.slideButtons[0].text}`,
        })
      }

      if (index == 1) {
        this.data.formConfig.forEach(function (config) {

          if (config.tag == 'userid') {
            let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
            let num = parseInt(prolist.userid)
            // console.log(num);
            // console.log(e);
            // console.log(config.prop);
            // console.log(e.currentTarget.dataset.data[config.prop]);
            // console.log(num!=e.currentTarget.dataset.data[config.prop])

            // console.log(prolist.userid != e.currentTarget.dataset.data[config.prop]);
            if (num != e.currentTarget.dataset.data[config.prop]) {
              wx.showToast({
                title: '编辑失败,没有权限',
                icon: 'none',
                duration: 1500,
                mask: true
              })
              flag = true
            }
          }
        })
        if (flag) return

        let data = JSON.stringify(e.currentTarget.dataset.data)
        let formConfig = JSON.stringify(this.properties.slide.formConfig)
        let net = JSON.stringify(this.data.net.update)
        wx.navigateTo({
          url: `/pages/edit/edit?formConfig=${formConfig}&data=${data}&net=${net}&titile=${this.data.slideButtons[0].text}`,
        })
      }



    },
    examine() {
      let obj = JSON.stringify(this.data.objProp)
      wx.navigateTo({
        url: `/pages/examine/examine?obj=${obj}`,
      })
    },
    look() {
     
    let data = JSON.stringify(this.data.objProp)
      wx.navigateTo({
        url: `/pages/mainlook/mainlook?data=${data}`,
      })
    },
    edit(e) {
      let {
        objProp
      } = this.data
      wx.navigateTo({
        url: `/pages/mainedit/mainedit?data=${JSON.stringify(objProp)}`,
      })
    },
    buttonTap(e) {
      let data = JSON.stringify({
        ...e.currentTarget.dataset.data,
        id: e.currentTarget.dataset.data.flowId
      })

      let d = JSON.parse(data);
      console.log(d);

      this.setData({
        objProp: {
          ...d
        }
      })
      let net = JSON.parse(JSON.stringify(this.data.net.select))
      var str = net.url.split("")

      var targetStr = str[str.length - 1]
      console.log(targetStr);

      if (targetStr == 2) {
        this.setData({
          typeShow: 2,
          show: true,
        })
      } else {
        switch (d.status) {
          case 1:
            this.setData({
              typeShow: 1,
              show: true,
            
            })
            break;
          case 2:
            this.setData({
              typeShow: 1,
              show: true,
            
            })
            break;
          case 3:
            this.setData({
              typeShow: 3,
              show: true,
              closeId: d.processId
            })
            break;
          case 4:
            wx.showToast({
              title: '已撤销无法操作',
              icon: 'none',
              duration: 1000,
            })
            break;

          default:
            break;
        }
       
      }









    },
    revoke() {
      let {
        closeId
      } = this.data;
      get(`/app/approvalRecord/revoke/${closeId}`).then(res => {
        if (res.success) {
          wx.showToast({
            title: '撤销成功',
            duration: 5000,
            icon: 'none',
            mask: true
          })
          this.setData({
            show: false,
          })
        }

        this.getList();

      })
    },
    addButtonTap(e) {


      let formConfig = JSON.stringify(this.data.formConfig)
      let net = JSON.stringify(this.data.net.insert)
      console.log(net);
      wx.navigateTo({
        url: `/pages/edit/edit?formConfig=${formConfig}&net=${net}`,
      })
    }
  }
})